package sideScrollerGame;
import java.awt.Color;
import java.util.Vector;

import main.Board;
import main.Sprite;
import player.Triangle;
import quadtree.DoubleDimension;
import quadtree.DoublePoint;
import quadtree.NewQuadTreeElement;

public class myBoard extends Board {
	
	boolean debug = false;
	Player player;
	
	public myBoard(boolean debug){
		super();
		super.setDebug(debug);
		this.debug = debug;
		gameInit();
	}
	
	public void gameInit(){
		DoublePoint p = new DoublePoint(300, 400);

		player = new Player(p);
		player.setGameTree(gameTree);
		gameTree.insertSprite(p, player);

		setBackground(Color.white);
		
	}
	public void handleInput(){
		try{
			gameHandleInput();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void gameHandleInput(){
		if(userInput.inputHandler.DOWN_PUSH == true){
			player.keyPressed("VK_DOWN");

		}
		if(userInput.inputHandler.UP_PUSH == true){
			player.keyPressed("VK_UP");

		}
		if(userInput.inputHandler.RIGHT_PUSH == true){
			player.keyPressed("VK_RIGHT");

		}	
		if(userInput.inputHandler.LEFT_PUSH == true){
			player.keyPressed("VK_LEFT");	
		}
		
		if(userInput.inputHandler.SPACE_PUSH == true){
			player.keyPressed("VK_SPACE");
			
		
		}
		if(userInput.inputHandler.SHIFT_PUSH == true){
			player.keyPressed("VK_SHIFT");

		}
		if(userInput.inputHandler.DOWN_RELEASE == true){
			player.keyReleased("VK_DOWN");
			userInput.inputHandler.DOWN_RELEASE = false;

		}
		if(userInput.inputHandler.UP_RELEASE == true){
			player.keyReleased("VK_UP");
			userInput.inputHandler.UP_RELEASE = false;
		}
		if(userInput.inputHandler.RIGHT_RELEASE== true){
			player.keyReleased("VK_RIGHT");
			userInput.inputHandler.RIGHT_RELEASE = false;
		}	
		if(userInput.inputHandler.LEFT_RELEASE == true){
			player.keyReleased("VK_LEFT");	
			userInput.inputHandler.LEFT_RELEASE = false;
		}
		if(userInput.inputHandler.DOWN_RELEASE == true){
			player.keyReleased("VK_DOWN");
			userInput.inputHandler.DOWN_RELEASE = false;

		}
	
		if(userInput.inputHandler.SPACE_RELEASE == true){
			player.keyReleased("VK_SPACE");

		}
		if(userInput.inputHandler.SHIFT_RELEASE == true){
			player.keyReleased("VK_SHIFT");
		}
		if(userInput.inputHandler.MOUSE_MOVED == true) {
			userInput.inputHandler.MOUSE_MOVED = false;
			mouseMoved(userInput.MouseX, userInput.MouseY);
		}
		if(userInput.inputHandler.MOUSE_PRESSED == true) {
			userInput.inputHandler.MOUSE_PRESSED = false;
			if(userInput.inputHandler.MOUSE_LEFT_CLICK) {
				userInput.inputHandler.MOUSE_LEFT_CLICK = false;
				mousePressed(userInput.MouseX, userInput.MouseY);
			}
			else if(userInput.inputHandler.MOUSE_RIGHT_CLICK) {
				userInput.inputHandler.MOUSE_RIGHT_CLICK = false;
				Triangle tmp = new Triangle(userInput.MouseX, userInput.MouseY, 50, 50, -2);
				tmp.setGameTree(gameTree);
				gameTree.insertSprite(new DoublePoint(userInput.MouseX, userInput.MouseY), 
						tmp);
				tmp = null;
			}
		}
	}
	
	public void mousePressed(int X, int Y) {
		//super.mouseClicked(e);
		// TODO Auto-generated method stub
		int offset = 75;
		int x = X-offset;
		int y = Y-offset;
	
		Vector<NewQuadTreeElement<Sprite>> checkElements;
		checkElements = gameTree.spritesTree.queryObject(new DoublePoint(x, y), 
				new DoubleDimension(offset*2,offset*2));
		for(int i = 0; i < checkElements.size(); i++) {
			try {
				Triangle spriter = (Triangle)checkElements.get(i).getElement();
				if(spriter.pointWithin(x+offset, y+offset)) {
					((Triangle)spriter).die();
				}
			} catch (Exception exc) {
				//do nothing
			}
		}
	}
	
	public void mouseMoved(int X, int Y) {
		// TODO Auto-generated method stub
		
	}
	

}
